// File: GridCanvas.java
// Purpose: This is a canvas thatfills itself witha  grid of lines
//          using a passed in LineDrawer type object and sets a
//          passed in textFiled with a performance report

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

public class GridCanvas extends JComponent {
	LineDrawer ld;
	TextField status;
	static long acc=0;
	BufferedImage bi;
 
 	public GridCanvas(LineDrawer ld, TextField status){
 		this.ld = ld;
 		this.status = status; 	
 	}
 	
 	public void test(){
		Rectangle r = getBounds();
	    bi = new BufferedImage(r.width,r.height,BufferedImage.TYPE_INT_RGB);			
	    Stopwatch watch = new Stopwatch();
	    for (int j=0;j<10;j++) { // do 10 times for stability	
			watch.reset().start();
			for (int i =0; i< (r.height-1)/2; i++){
				ld.drawLine(bi,0,i*2,r.width-1,i*2);
			}
			for (int  i=0; i<(r.width-1)/2;i++){				
				ld.drawLine(bi,i*2,0,i*2,r.height-1);			
	    	}	
	    	watch.stop();
	    	acc += watch.getElapsedTime();
	    }		    
	    status.setText("Time (ms) to do grid = "+(acc/10));
	    repaint();
 	}
 	
	public void paint(Graphics g) {
		g.drawImage(bi,0,0,this);
	}    
}	
		
